package cn.tedu.flower.manage.content.service.impl;

import cn.tedu.flower.manage.common.ex.ServiceException;
import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.web.OrderStatus;
import cn.tedu.flower.manage.common.web.ServiceCode;
import cn.tedu.flower.manage.content.dao.persist.mapper.OrderMainMapper;
import cn.tedu.flower.manage.content.dao.persist.repository.IOrderMainRepository;
import cn.tedu.flower.manage.content.pojo.entity.OrderMain;
import cn.tedu.flower.manage.content.pojo.param.OrderMainAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.OrderMainUpdateNewParam;
import cn.tedu.flower.manage.content.pojo.vo.GoodsListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.OrderMainListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.OrderMainStandardVO;
import cn.tedu.flower.manage.content.service.IOrderMainService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OrderMainServiceImpl implements IOrderMainService {
    @Value("${flower-store.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    IOrderMainRepository orderMainRepository;


    @Override
    public void addNew(OrderMainAddNewParam orderMainAddNewParam) {

       //TODO 需判断商品库存  想好后再写

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除订单】的请求,参数:{}",id);
        OrderMainStandardVO orderMainStandardVO = orderMainRepository.mainStandardById(id);
        if (orderMainStandardVO==null) {
            String message="删除订单失败,该订单数据已不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int rows = orderMainRepository.deleteById(id);
        if (rows!=1) {
            String message="删除订单失败,服务器繁忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }

    }

    @Override
    public void update(OrderMainUpdateNewParam orderMainUpdateNewParam) {
        log.debug("开始处理【修改订单】的请求,参数:{}",orderMainUpdateNewParam);
        OrderMainStandardVO orderMainStandardVO = orderMainRepository.mainStandardById(orderMainUpdateNewParam.getId());
        if (orderMainStandardVO==null) {
            String message="修改订单失败,该订单数据已不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        OrderMain orderMain=new OrderMain();
        BeanUtils.copyProperties(orderMainUpdateNewParam,orderMain);
        int rows = orderMainRepository.update(orderMain);
        if (rows!=1) {
            String message="修改订单失败,服务器繁忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }



    }

    @Override
    public PageData<OrderMainListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询订单列表】的业务，页码：{}", pageNum);
        PageData<OrderMainListItemVO> pageData = orderMainRepository.list(pageNum, defaultQueryPageSize);
        return pageData;

    }




}
